package net.sh.dpinfrastructure.exception;


/**
 * 
 * @author Lancer
 * @date 2015年8月2日下午9:23:11 异常类
 *
 */
public class DPException extends RuntimeException {
    
    private static final long serialVersionUID = 1L;

    /**
     * 模块（两位十六进制字符）
     * 
     * @author Lancer
     *
     */
    public enum Modal {

        /**
         * 未知模块
         */
        UNKNOWN(0xff),

        /**
         * 登录
         */
        LOGIN(0x00),

        /**
         * 注册
         */
        REGISTER(0x01),

        /**
         * 注销
         */
        LOGOUT(0x02),

        /**
         * 框架
         */
        DATAPLATFORM(0x03);
        
        /**
         * 编号
         */
        private int code;

        private Modal(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }

    }

    /**
     * 级别（两位十六进制字符）
     * 
     * @author Lancer
     *
     */
    public enum Level {

        /**
         * 未知
         */
        UNKNOWN(0xff),

        /**
         * 错误
         */
        ERROR(0x00),

        /**
         * 警告
         */
        WARN(0x01),

        /**
         * 一般
         */
        NORMAL(0x02),

        /**
         * 提醒
         */
        REMIND(0x03),

        /**
         * 功能
         */
        FUNCTION(0x04);

        /**
         * 编号
         */
        private int code;

        private Level(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }

    }

    /**
     * 类型（两位十六进制字符）
     * 
     * @author Lancer
     *
     */
    public enum Type {

        /**
         * 未知错误
         */
        UNKNOWN(0xff),

        /**
         * 数据库插入
         */
        DB_ADD(0x00),

        /**
         * 数据库更新
         */
        DB_UPDATE(0x01),

        /**
         * 数据库保存（插入或更新）
         */
        DB_SAVE(0x02),
        
        /**
         * 数据库物理删除
         */
        DB_PHYSICAL_DELETE(0x03),

        /**
         * 数据库逻辑删除
         */
        DB_LOGICAL_DELETE(0x04),

        /**
         * 查询错误
         */
        DB_SELECT(0x05),
        
        /**
         * 数据库驱动
         */
        DB_DRIVER(0x07),
        
        /**
         * 数据库连接失败
         */
        DB_CONNECTION(0x08),

        /**
         * 网络错误
         */
        NETWORK(0x06);

        /**
         * 编号
         */
        private int code;

        private Type(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }
    }

    private Level level;

    private Modal modal;

    private Type type;

    private String code;

    private Throwable originalException;
    
    public DPException(Level level, Modal modal, Type type, String message, Throwable e) {
        super(message, e);
        this.level = level;
        this.modal = modal;
        this.type = type;
        this.originalException = e;
    }
    
    public DPException(Level level, Modal modal, Type type, String message) {
        super(message);
        this.level = level;
        this.modal = modal;
        this.type = type;
    }

    public DPException(String message) {
        this(Level.UNKNOWN, Modal.UNKNOWN, Type.UNKNOWN, message);
    }

    public DPException(String message, Throwable t) {
        this(Level.UNKNOWN, Modal.UNKNOWN, Type.UNKNOWN, message, t);
    }
    
    public DPException(Throwable t) {
        this(Level.UNKNOWN, Modal.UNKNOWN, Type.UNKNOWN, t.getMessage(), t);
    }
    
    public DPException() {
        this(Level.UNKNOWN, Modal.UNKNOWN, Type.UNKNOWN, "未知错误");
    }

    public Level getLevel() {
        return level;
    }

    public Modal getModal() {
        return modal;
    }

    public Type getType() {
        return type;
    }

    public String getCode() {
        return Integer.toHexString(level.getCode()) + Integer.toHexString(modal.getCode())
                + Integer.toHexString(type.code);
    }

    public Throwable getOriginalException() {
        return originalException;
    }
    
}
